var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var base64 = require('../../utils/Base64.js');
Page({
  data: {
    util: util,
    screen_h: 480,
    token: '',
    uid: '',
    userinfo: '',

    guiListData: '',
    colorSet: '',

  },

  isDebug: false,//这个是调试机柜
  demo: false,//这个是无二维码的情况下进入购买演示

  mendianId: '',//门店id

  allGuiListData: '',

  onLoad: function (options) {

    var that = this;
    that.isDebug = options.debug == 'true' ? true : false;
    that.demo = options.demo == 'true' ? true : false;
    that.mendianId = options.mendianid ? options.mendianid : '';

    wx.getSystemInfo({
      success: function (res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight
        });
      }
    });

    that.getLoginUserInfo();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });
  },

  onShow: function () {
    this.getGuiListData();
  },

  //设置titlebar 样式
  resetNavColor: function () {
    var bg_color = '#f0f0f0';
    var fg_color = '#000000';
    if (this.data.colorSet) {
      bg_color = this.data.colorSet.titlebg;
      fg_color = this.data.colorSet.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  //获取所有售货机信息
  getGuiListData: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getguilist",
        "mendianid": this.mendianId ? this.mendianId : ""
      },
      success: function (responseData) {
        console.log("所有售货机列表：", responseData);
        if (responseData.data.msg) {

        } else {
          let listData = responseData.data;
          let onLineDev = [];
          let offLineDev = [];
          listData.forEach(function (m, i) {
            let start = m.guiid.length - 4;
            let showguiid = m.guiid.substr(start, 4);
            m.title = m.title + showguiid;
            if (m.isalive) {
              onLineDev.push(m);
            } else {
              offLineDev.push(m);
            }
          });
          listData = onLineDev.concat(offLineDev);
          that.allGuiListData = listData;
          that.setData({
            guiListData: listData
          })
        }
      }
    });
  },

  itemTouchAction: function (e) {
    var guiId = e.currentTarget.dataset.guiid;
    var path = "/pages/manage_gui/manage_gui?guiid=" + guiId;
    if (this.isDebug) {
      path = "/pages/gui_debug/gui_debug?guiid=" + guiId;
    } else if (this.demo) {
      path = "/pages/gui/gui?guiid=" + guiId;
    }
    wx.navigateTo({
      url: path,
    })
  },

  goAtherPage: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.link
    })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({
      url: url
    })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);
  },



  getLoginUserInfo: function () {
    var that = this;
    //读取登录用户信息
    app.LoginUserInfo(function (res) {
      console.log("登录用户信息：", res);
      if (res && res != -1) {
        that.setData({
          uid: res.uid,
          token: res.token,
          userinfo: res
        });
      } else {
        wx.hideLoading();
      }
    }, '');
  },

  inputVal: function (e) {
    var that = this;
    let searchVal = e.detail.value;
    if (searchVal && searchVal.length > 0) {
      let selGuiList = [];
      this.allGuiListData.forEach(function (m) {
        if (m.title.indexOf(searchVal) >= 0) {
          selGuiList = selGuiList.concat(m);
        }
      });
      this.setData({
        guiListData: selGuiList
      })
    } else {
      this.setData({
        guiListData: that.allGuiListData
      })
    }

  },

  search: function () {

  }

})